const Sequelize = require('sequelize')
const {sequelize} = require('@/mysql/utils')
const {Project} = require('@/mysql/project')

// 创建用户实体
const ScheduleTask = sequelize.define('schedule', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.STRING,
            allowNull: false
        },
        description: {
            type: Sequelize.TEXT
        },
        status: {
            type: Sequelize.INTEGER,
            defaultValue: 0
        },
        mark_date: {
            type: Sequelize.DATE(6)
        },
        create_user: {
            type: Sequelize.STRING
        },
        update_user: {
            type: Sequelize.STRING
        },
        expire_time: {
            type: Sequelize.DATE(6)
        },
        date: {
            type: Sequelize.DATE(6),
            allowNull: false
        },
        forUser: {
            type: Sequelize.STRING
        },
        deleted: {
            type: Sequelize.BOOLEAN,
            defaultValue: false
        }
    }
);
ScheduleTask.belongsTo(Project)
ScheduleTask.sync({force: false})
// /**插入新项目类型数据（添加） */
ScheduleTask.insertData = async (project_id, name, description, mark_date, expire_time, update_user) => {
    await ScheduleTask.sync({force: false})
    // 表已创建
    return ScheduleTask.create({
        name,
        description,
        projectProjectId: project_id,
        mark_date, expire_time, date: (new Date()).toLocaleString(),
        update_user

    })
}

/** 更新项目数据 */
ScheduleTask.updateData = async (id, name, description, update_user, expire_time, mark_date) => {
    return ScheduleTask.update({
        name, description, update_user, expire_time, mark_date
    }, {
        where: {id}
    })
}
/**
 * @functionName: 查找所有项目日程任务
 * @param:  ScheduleTask
 * @date 2019/5/19 10:28
 * @description 描述
 * @version 1.0
 */
ScheduleTask.getAllScheduleTasks = async (projectProjectId, deleted = false) => {
    return ScheduleTask.findAll({where: {projectProjectId, deleted}})
}

export {ScheduleTask}

